package com.bw.wxy.fragment;

import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.wxy.R;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.mvp.base.view.BaseFragment;
import com.bw.wxy.mvp.contract.LREContract;
import com.bw.wxy.mvp.di.DaggerLREComponent;
import com.bw.wxy.mvp.di.LREModules;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.RecommendEntity;
import com.bw.wxy.mvp.presenter.LREPresenter;
import com.bw.wxy.mvp.view.pople.adapter.RecommendAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fragment_Recommend extends BaseFragment<LREPresenter> implements LREContract.ILREView,
        BaseQuickAdapter.OnItemChildClickListener, OnRefreshLoadMoreListener {

    private RecyclerView recommend_rv;
    private LinearLayoutManager manager;
    private RecommendAdapter adapter;
    private SmartRefreshLayout recommendSmart;
//    private RecyclerView recommendRv;

    @Override
    public int bindLayout() {
        return R.layout.fragment__recommend;
    }

    @Override
    public void initView() {
        recommend_rv = f(R.id.recommend_rv);
        manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recommend_rv.setLayoutManager(manager);
        adapter = new RecommendAdapter(new ArrayList<>());
        adapter.setOnItemChildClickListener(this);
        recommend_rv.setAdapter(adapter);
        recommendSmart = f(R.id.recommend_smart);
        recommendSmart.setEnableLoadMore(true);
        recommendSmart.setEnableRefresh(true);
        recommendSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        recommendSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        recommendSmart.setOnRefreshLoadMoreListener(this);
    }

    @Override
    public void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this)).build().lreRecommend(this);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        if (baseEntity instanceof RecommendEntity) {
            RecommendEntity recommendEntity = (RecommendEntity) baseEntity;
            adapter.setNewData(recommendEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        RecommendEntity recommendEntity= (RecommendEntity) baseEntity;
        if (recommendEntity.getValues()==null){
            Toast.makeText(getActivity(), "刷新失败", Toast.LENGTH_SHORT).show();
            return;
        }
        adapter.setNewData(recommendEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        RecommendEntity recommendEntity= (RecommendEntity) baseEntity;
        if (recommendEntity.getValues()==null){
            Toast.makeText(getActivity(), "最后一页", Toast.LENGTH_SHORT).show();
            return;
        }
        adapter.addData(recommendEntity.getValues());
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        List<RecommendEntity.Values> list = (List<RecommendEntity.Values>) adapter.getData();
//        ToastUtils.showMsg("当前点击:"+list.get(position).news_title);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestLoad(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestRefresh(map);
    }
}